package com.ls.fw.office.itext.itext.handler;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.ls.fw.office.itext.itext.builder.ISignatureBuilder;
import com.ls.fw.office.itext.itext.builder.ParagraphBuilder;

public class Handler implements IHandler{
	
	private Document document = null;
	
	private String path = "";
	
    private ISignatureBuilder signatureBuilder;

    private OutputStream out = null;
    
    private InputStream in = null;
    
    /**
	 * Constructs a new <CODE>Document</CODE> -object.
     * @throws DocumentException 
     * @throws FileNotFoundException 
	 */
    public Handler(String path) throws FileNotFoundException, DocumentException {
        this(path,PageSize.A4);
    }
    
    
    public Handler(String path, boolean isPortraitA4) throws FileNotFoundException, DocumentException {
    	 this(path,isPortraitA4 ? PageSize.A4 : PageSize.A4.rotate());
    }
	/**
	 * Constructs a new <CODE>Document</CODE> -object.
	 *
	 * @param pageSize
	 *            the pageSize
	 * @throws DocumentException 
	 * @throws FileNotFoundException 
	 */
    public Handler(String path,Rectangle pageSize) throws FileNotFoundException, DocumentException {
        this(path,pageSize, 36, 36, 36, 36);
    }

	/**
	 * Constructs a new <CODE>Document</CODE> -object.
	 *
	 * @param pageSize
	 *            the pageSize
	 * @param marginLeft
	 *            the margin on the left
	 * @param marginRight
	 *            the margin on the right
	 * @param marginTop
	 *            the margin on the top
	 * @param marginBottom
	 *            the margin on the bottom
	 * @throws DocumentException 
	 * @throws FileNotFoundException 
	 */
	public Handler(String path,Rectangle pageSize, float marginLeft, float marginRight,
			float marginTop, float marginBottom) throws FileNotFoundException, DocumentException {
		this(path,new Document(pageSize,marginLeft,marginRight,marginTop,marginBottom));
	}
	
	public Handler(String path,Document document) throws FileNotFoundException, DocumentException {
		 this(new FileOutputStream(path),document) ;
	}
	
	public Handler(OutputStream out,Document document) throws FileNotFoundException, DocumentException {
		 this.document = document;
		 this.out = out;
	}
	
	public Handler(OutputStream out) throws FileNotFoundException, DocumentException {
		 this(out,new Document(PageSize.A4)) ;
	}
	
	public Document getDocument(){
		return this.document;
	}
	
	public void signWithAlias(String alias) throws Exception {
        this.close();
        this.signatureBuilder.sign(alias);
    }
	
	public Handler addSignatureField(ISignatureBuilder signatureBuilder){
        this.signatureBuilder = signatureBuilder;
        return this;
    }
	public Handler add(Element table ) throws DocumentException{
		this.document.add(table);
		return this;
	}
	
	 public Handler addParagraph(ParagraphBuilder paragraphBuilder) throws DocumentException {
	        document.add(paragraphBuilder.getParagraph(null, null));
	        return this;
	 }
	
	public Handler withAuthor(String author){
	        document.addAuthor(author);
	        return this;
	}
	
	
	public Handler newPage() throws DocumentException {
        document.newPage();
        return this;
    }
	public void open(){
		if(document!=null && !document.isOpen()){
			document.open();
		}
    }
	
	public void close(){
		if(document!=null && document.isOpen()){
			document.close();
		}
    }

	@Override
	public String getPath() {
		return this.path;
	}


	public OutputStream getOut() {
		return out;
	}


	public void setOut(OutputStream out) {
		this.out = out;
	}


	public InputStream getIn() {
		return in;
	}


	public void setIn(InputStream in) {
		this.in = in;
	}
 
}
